package seismosurfer.gui.menuitem;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.beancontext.BeanContext;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import seismosurfer.data.QuakeData;
import seismosurfer.data.constants.QueryNames;
import seismosurfer.gui.GUIConstants;
import seismosurfer.http.HttpGateway;

import com.bbn.openmap.LightMapHandlerChild;
import com.bbn.openmap.MapHandler;
import com.bbn.openmap.gui.OMComponentPanel;
import com.bbn.openmap.gui.WindowSupport;

/**
 * A check box menu item that controls if you
 * can insert a document related to an earthquake,
 * by clicking upon it on the map.
 *
 */
public class AddDocumentMenuItem extends JCheckBoxMenuItem implements
        LightMapHandlerChild, ActionListener, QueryNames, GUIConstants {
    
    private static final long serialVersionUID = 5938960115925003651L;

    protected WindowSupport ws;

    protected MapHandler mapHandler = null;

    public static final String menuItemName = "Add Document";

    protected static final String addDoc = "Insert a document related to this quake";

    protected static final String addURL = "Insert a URL related to this quake";
    

    protected JRadioButton docButton = new JRadioButton(addDoc);

    protected JRadioButton urlButton = new JRadioButton(addURL);

    protected AddDocumentPanel adp;
   

    public AddDocumentMenuItem() {
        super(menuItemName);
        adp = new AddDocumentPanel(this);

    }

    public void setMapHandler(BeanContext in_mapHandler) {
        if (in_mapHandler instanceof MapHandler) {
            mapHandler = (MapHandler) in_mapHandler;
        }
        setEnabled(mapHandler != null);
    }

    public MapHandler getMapHandler() {
        return mapHandler;
    }

    public void findAndInit(Object someObj) {

        if (someObj instanceof MapHandler) {
            setMapHandler((MapHandler) someObj);
            getMapHandler().add(this);
        }        

    }

    public void findAndUndo(Object someObj) {

        if (someObj instanceof MapHandler) {
            getMapHandler().remove(this);
            setMapHandler(null);
        }
    }

    protected QuakeData qd;
    
    /**
     * Shows a dialog box to select the type
     * of information you`re going to add.
     * 
     * @param qd the related QuakeData
     */
    public void showRadioDlg(QuakeData qd) {

        this.qd = qd;

        System.out.println("Quake ID for OMGraphic: " + qd.getQuakeID());

        if (ws == null) {
            ws = new WindowSupport(adp, "Add Document or URL");
        }
        MapHandler mh = getMapHandler();
        Frame frame = null;
        if (mh != null) {
            frame = (Frame) mh.get(java.awt.Frame.class);
        }
        ws.displayInWindow(frame);
    }

    public void actionPerformed(ActionEvent ae) {

        String command = ae.getActionCommand();

        if ((command.equals(OK)) && (ws != null)) {

            if (docButton.isSelected()) {
                showPage(DOC);
            } else if (urlButton.isSelected()) {
                showPage(URL);
            } else {
                System.out.println("None radio button selected!!!");
            }

            ws.killWindow();
        }

        else if ((command.equals(CANCEL)) && (ws != null)) {
            ws.killWindow();
        }

    }

    protected void showPage(int type) {
        HttpGateway.showDocument(qd.getQuakeID(), type);
    }

    class AddDocumentPanel extends OMComponentPanel {
        
        private static final long serialVersionUID = 3919588282500226020L;

        static final String addDoc = "Insert a document related to this quake";

        static final String addURL = "Insert a URL related to this quake";

        public AddDocumentPanel(ActionListener buttonsListener) {
            super();
            createWidgets(buttonsListener);
        }

        protected void createWidgets(ActionListener buttonsListener) {

            setLayout(new BorderLayout());

            // Create the radio buttons.
            docButton = new JRadioButton(addDoc);
            docButton.setSelected(true);

            urlButton = new JRadioButton(addURL);

            // Group the radio buttons.
            ButtonGroup group = new ButtonGroup();
            group.add(docButton);
            group.add(urlButton);

            JPanel radioPanel = new JPanel(new GridLayout(0, 1));
            radioPanel.add(docButton);
            radioPanel.add(urlButton);

            // bottomPanel consists of 2 buttons: OK and Cancel.
            JPanel bottomPanel = new JPanel();            

            JButton b1 = new JButton("OK");
            b1.addActionListener(buttonsListener);
            b1.setActionCommand(OK);
            bottomPanel.add(b1);

            JButton b2 = new JButton("Cancel");
            b2.addActionListener(buttonsListener);
            b2.setActionCommand(CANCEL);
            bottomPanel.add(b2);

            add(radioPanel, BorderLayout.CENTER);
            add(bottomPanel, BorderLayout.SOUTH);

        }

    }

}
